package com.useeinfo.oa.modules.task.entity;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.enums.AuditTypeEnum;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.Date;

/**
 * 任务工单-审核记录表
 */
@Entity
@Table(name = "task_audit_order")
public class TaskAuditOrder extends SuperClassEntity<TaskAuditOrder> {

    /**
     * 关联工单
     */
    @ManyToOne
    @JoinColumn(name = "task_order_id")
    private TaskOrder taskOrder;
    /**
     * 审核人
     */
    @ManyToOne
    @JoinColumn(name = "audit_user_id")
    private SystemUser auditUser;
    /**
     * 审核时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date auditDate;
    /**
     * 不通过原因
     */
    private String reason;
    /**
     * 当前审核结果
     */
    private boolean pass = true;
    /**
     * 备注
     */
    private String remark;

    /**
     * 当前审核等级
     */
    private int level = 1;
    /**
     * 是否有效，最终审核通过并且最新的审核通过的数据，算有效
     */
    private boolean effective = true;
    /**
     * 所属企业
     */
    @ManyToOne
    @JoinColumn(name = "enterprise_id")
    private Enterprise enterprise;

    public SystemUser getAuditUser() {
        return auditUser;
    }

    public void setAuditUser(SystemUser auditUser) {
        this.auditUser = auditUser;
    }

    public Date getAuditDate() {
        return auditDate;
    }

    public void setAuditDate(Date auditDate) {
        this.auditDate = auditDate;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Boolean getPass() {
        return pass;
    }

    public void setPass(Boolean pass) {
        this.pass = pass;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public Boolean getEffective() {
        return effective;
    }

    public void setEffective(Boolean effective) {
        this.effective = effective;
    }

    public boolean isPass() {
        return pass;
    }

    public void setPass(boolean pass) {
        this.pass = pass;
    }

    public boolean isEffective() {
        return effective;
    }

    public void setEffective(boolean effective) {
        this.effective = effective;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    public TaskOrder getTaskOrder() {
        return taskOrder;
    }

    public void setTaskOrder(TaskOrder taskOrder) {
        this.taskOrder = taskOrder;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("orderName", StringConverters.null2String(getTaskOrder().getOrderName()));
        jsonObject.put("serial", StringConverters.null2String(getTaskOrder().getSerial()));
        jsonObject.put("auditUser", StringConverters.null2String(getAuditUser().getUserName()));
        jsonObject.put("auditDate", StringConverters.null2String(getAuditDate()));
        jsonObject.put("pass",StringConverters.null2String(getPass()?"通过":"驳回"));
        jsonObject.put("reason", StringConverters.null2String(getReason()));
        jsonObject.put("remark", StringConverters.null2String(getRemark()));
        jsonObject.put("passStatus",getPass());
        jsonObject.put("level", StringConverters.null2String(getLevel()));
        jsonObject.put("enterpriseName", getEnterprise() != null ? getEnterprise().getEnterpriseName() : "");
        return jsonObject;
    }
}
